package config

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/mitchellh/mapstructure"
	"idle/utils/configUtls"
	"log"
)

// J-角色升级表.xlsx
type HeroLvUpExpConfig struct {
	Sid int `json:"Sid"` // Sid
	Lv  int `json:"Lv"`  // 等级
	Exp int `json:"Exp"` // 所需经验
}

var heroLvUpExpConfigList []*HeroLvUpExpConfig
var heroLvUpExpConfigMap map[int]*HeroLvUpExpConfig

func GetHeroLvUpExpConfig(Sid int) *HeroLvUpExpConfig {
	if v, ok := heroLvUpExpConfigMap[Sid]; ok {
		return v
	}
	return nil
}

func GetAllHeroLvUpExpConfig() []*HeroLvUpExpConfig {
	result := make([]*HeroLvUpExpConfig, 0)
	for _, c := range heroLvUpExpConfigList {
		result = append(result, c)
	}
	return result
}

func loadHeroLvUpExpConfig(filePath string) {
	xlsx, err := excelize.OpenFile(filePath)
	if err != nil {
		log.Fatal(err)
	}
	sheetName := xlsx.GetSheetName(1)
	rows := xlsx.GetRows(sheetName)

	heroLvUpExpConfigMap = make(map[int]*HeroLvUpExpConfig)
	heroLvUpExpConfigList = make([]*HeroLvUpExpConfig, 0)

	for _, row := range rows[4:] {
		data := make(map[string]interface{})
		for j, colCell := range row {
			fieldName := rows[1][j]
			if fieldName != "" {
				data[rows[1][j]] = configUtls.GetValueByType(rows[2][j], colCell)
			}
		}
		var config HeroLvUpExpConfig
		err := mapstructure.Decode(data, &config)
		if err != nil {
			log.Fatal(err)
			return
		}

		heroLvUpExpConfigMap[config.Sid] = &config
		heroLvUpExpConfigList = append(heroLvUpExpConfigList, &config)
	}
}
